import { getPlanByPId } from '@/api/plan'
import { getTicketsByPId } from '@/api/ticket'
const state = {
  seatList: [],
  plan: {},
  selectedSeat: [],
  totalPrice: 0
}
const getters = {
  seatArray: state => {
    const result = []
    for (let i = 0; i < state.seatList.length; i++) {
      const row = state.seatList[i].row
      const col = state.seatList[i].col
      if (result[row - 1] === undefined) {
        result[row - 1] = []
      }
      result[row - 1][col - 1] = state.seatList[i].ticketStatus
    }
    return result
  }
}
const mutations = {
  setSeatList (state, seatList) {
    state.seatList = seatList
  },
  setTotalPrice (state, totalPrice) {
    state.totalPrice = totalPrice
  },
  setPlan (state, plan) {
    state.plan = plan
  },
  setSelectedSeat (state, selectedSeat) {
    state.selectedSeat = selectedSeat
  },
  SET_SELECTED_SEAT (state, selectedSeat) {
    state.selectedSeat = selectedSeat
  }
}
const actions = {
  getTicketsByPId ({ commit }, pId) {
    return new Promise((resolve, reject) => {
      getTicketsByPId(pId).then(res => {
        const { data } = res
        commit('setSeatList', data)
        resolve(data)
      }).catch(err => {
        reject(err)
      })
    })
  },
  getPlanByPId ({ commit }, pId) {
    return new Promise((resolve, reject) => {
      getPlanByPId(pId).then(res => {
        const { data } = res
        commit('setPlan', data)
        resolve(data)
      }).catch(err => {
        reject(err)
      })
    })
  },
  setSelectedList ({ commit }, selectedSeat) {
    commit('setSelectedSeat', selectedSeat)
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
